<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
use app\common\library\Letter;
class Channel extends Model{
   protected $name = 'List';
   protected function setListdirAttr($value,$data){
	 if (empty($value)){
		     $Letter = new Letter();
		     $listdir=$Letter->getletters(trim($data['list_name']),'list');
		     return $listdir;
	     }else{
		     return trim($value);
	     }
    }
	public function mcat(){
        return $this->hasMany('Mcat','m_list_id')->order('m_order asc');
    }	
	//生成前台分类缓存
    public function list_cache(){
		$list=db('List')->join('model m','m.id=list_sid')->order('list_id asc,list_oid')->column('list_id,list_pid,list_oid,list_sid,list_name,list_dir,m.name,m.title,list_jumpurl','list_id');
		$listall=db('List')->alias('l')->join('model m','m.id=list_sid')->order('list_id asc,list_oid')->column('l.*,m.id,m.name,m.title','list_id');
		F('_data/list_all',$listall);
		F('_data/list',$list);
		F('_data/listtree',$this->list_to_tree($list,'list_id','list_pid','son',0));
		$modlist=config('model');
		foreach ($modlist as $k=>$v){
		    F('_data/list'.$v['name'],list_search(F('_data/listtree'),'list_sid='.$v['id']));
		}
		Cache::clear('list');
		model('mcat')->delCache();
    }
    public function list_to_tree($list, $pk='id',$pid = 'pid',$child = '_child',$root=0){
    // 创建Tree
    $tree = array();
    if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
            // 判断是否存在parent
            $parentId = $data[$pid];
            if ($root == $parentId) {
                $tree[] =& $list[$key];
            }else{
                if (isset($refer[$parentId])) {
                    $parent =& $refer[$parentId];
                    $parent[$child][] =& $list[$key];
                }
            }
        }
    }
    return $tree;
   }
    public function get_type_cid($sid,$vid=''){
		$channel=model('channel');
		$config=config('');
		$system=parse('textarea',config('system.system_'.$config['model'][$sid]['name'].'_cidarray'));
		if(($sid==4 && $system[0]=='all') || $sid==5 || $sid==6){
		    $list=db('list')->where(['list_pid'=>0,'list_sid'=>$sid])->find();
			if(!empty($list)){
	            return $list['list_id'];	
			}else{
				$list=model('channel');
				$data['list_name']=$config['model'][$sid]['title'];
				$data['list_skin']=$config['model'][$sid]['template_list_skin'];
		        $data['list_skin_detail']=$config['model'][$sid]['template_list_detail'];
		        $data['list_sid']=$sid;
				$data['list_dir']='';
                $data['list_pid']=0;
                $data['list_oid']=db('list')->where('list_pid',0)->max('list_oid')+1;					
		        $data['list_status']=1;	
				$channel->save($data);
				$channel->list_cache();
				return $channel->list_id;
			}
		}
		if($sid==4 && $system[0]=='top' || empty($system)){
		    $cid=db('vod')->where('vod_id',$vid)->value('vod_cid');
            if($system[0]=='top')	{		
			    $list_name=getlistname($cid,'list_name_big');
			}else{		
			    $list_name=getlistname($cid,'list_name');
			}
			$id=db('list')->where(array('list_sid'=>$sid,'list_name'=>$list_name.'剧情'))->value('list_id');
			if(!empty($id)){
					return $id;
			}else{
				$pid=db('list')->where(['list_pid'=>0,'list_sid'=>$sid])->value('list_id');
				if(!empty($pid)){
				    $data['list_name']=$list_name.'剧情';
				    $data['list_skin']=$config['model'][$sid]['template_list_skin'];
		            $data['list_skin_detail']=$config['model'][$sid]['template_list_detail'];
		            $data['list_sid']=$sid;
				    $data['list_dir']='';
                    $data['list_pid']=$pid;
                    $data['list_oid']=db('list')->where('list_pid',$pid)->max('list_oid')+1;				
		            $data['list_status']=1;	
				    $channel->save($data);
					$channel->list_cache();
				    return $channel->list_id;					
				}else{
				    $big['list_name']=$config['model'][$sid]['title'];
				    $big['list_skin']=$config['model'][$sid]['template_list_skin'];
		            $big['list_skin_detail']=$config['model'][$sid]['template_list_detail'];
		            $big['list_sid']=$sid;
				    $big['list_dir']='';
                    $big['list_pid']=0;
                    $big['list_oid']=db('list')->where('list_pid',0)->max('list_oid')+1;				
		            $big['list_status']=1;	
				    $channel->save($big);
					if($channel->list_id){
				        $data['list_name']=$list_name.'剧情';
				        $data['list_skin']=$config['model'][$sid]['template_list_skin'];
		                $data['list_skin_detail']=$config['model'][$sid]['template_list_detail'];
		                $data['list_sid']=$sid;
				        $data['list_dir']='';
                        $data['list_pid']=$channel->list_id;
                        $data['list_oid']=db('list')->where('list_pid',$channel->list_id)->max('list_oid')+1;				
		                $data['list_status']=1;	
				        $channel->isUpdate(false)->data($data,true)->save();
                        $channel->list_cache();						
                        return $channel->list_id;						
					}
				}
				
			}
		}
		if($sid==4 && !empty($system)){
			$cid=db('vod')->where('vod_id',$vid)->value('vod_cid');
			if(!empty($system[$cid])){
			    return $system[$cid];
			}	
		}
    }
	public function listData($tag){	
		    if(!is_array($tag)) {
                $tag=json_decode($tag, true);
            }
		    $tag['limit']= !empty($tag['limit']) ? $tag['limit'] : '*';
            if(!in_array($tag['order'],['list_id asc','list_id desc','list_oid desc','list_oid asc'])){
                $tag['order'] = 'list_oid asc';
            }			
		    if(!empty($tag['sid'])){
		        $sid = explode(',',$tag['sid']);
		        if(count($sid)>1) {
			        $where['list_sid'] = array('in',$tag['sid']);
		        }else{
			        $where['list_sid'] = array('eq',$sid[0]);
		        }
		    }
	        if(!empty($tag['ids'])){
		        $ids = explode(',',$tag['ids']);
		        if(count($ids)>1) {
			        $where['list_id'] = array('in',$tag['ids']);
		        }else{
			        $where['list_id'] = array('eq',$ids[0]);
		        }
	        }
            if($tag['pid']=='all'){
				$where['list_pid']=array('neq',0);
			}elseif($tag['pid']==0){
		        $where['list_pid']=array('eq',0);	
		    }else{
			    $where['list_pid']=array('eq',$tag['pid']);
		    }			
		    $where['list_status'] = array('eq',1);
		    $limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
            $list=db('List')->where($where)->cache(md5(implode('_',$tag)),0,'list')->limit($limit_str)->order($tag['order'])->select();
		    return $list;	
		}


}